package com.edgar.paysdk

import android.content.Context
import android.content.res.Resources
import android.util.Log

/**
 * Created by edgar on 18-1-20.
 */
abstract class PayPlatform(context: Context) {

    val mContext:Context = context.applicationContext
    var mPayListener:PayListener? = null

    companion object {
        @JvmStatic val TAG = "PayPlatform"
    }
    fun getResource():Resources = mContext.resources

    fun setPayListener(payListener: PayListener){
        mPayListener = null
        mPayListener = payListener
    }

    fun pay(payReq: PayReq):Boolean{
        if (isMustClient() && !isInstallApp()){
            mPayListener?.onError(this,AppNoInstallException("未安装${getClientName()}客户端"))
            return false
        }
        Log.d(TAG,"PayReq params:${payReq.toString()}")
        mPayListener?.onStartPay(this)
        val result = doPay(payReq)
        payReq.release()
        return result
    }

    internal open fun isMustClient():Boolean = false
    internal open fun isInstallApp():Boolean = false
    open fun getClientName():String = ""

    abstract fun getPayType():PayType
    abstract internal fun doPay(payReq: PayReq):Boolean
}